package com.hongxun.design.sqlite.popwind;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.bean.BitTypeTabItemBean;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.ui.headandbottonpanel.HeadSecondControlPanel;
import com.hongxun.design.utils.DataUtil;
import com.lidroid.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cqcqwu 项目手动添加材料
 */

public class QuickModelAddPW extends PopupWindow {
    private Handler handler;
    private Activity activity;

    /**
     * 0activity
     * 1handler
     * 2yesList1
     */
    public QuickModelAddPW(Object... obj) {
        this.activity = (Activity) obj[0];
        this.handler = (Handler) obj[1];

        View view = LayoutInflater.from(activity).inflate(R.layout.pop_quickmodel, null);
        ViewHolder holder = new ViewHolder(view);
        setContentView(view);
        setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
        setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        setBackgroundDrawable(dw);
        setAnimationStyle(R.style.pop_ranim);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }

    class ViewHolder {
        EditText popFixEt1;
        EditText popFixEt2;
        TextView popTv1;
        CheckBox checkBox1;
        CheckBox checkBox2;
        CheckBox checkBox3;
        LinearLayout layout;

        ArrayList<String> list;
        List<BitTypeTabItemBean> beanList;

        ViewHolder(View view) {
            popFixEt1 = (EditText) view.findViewById(R.id.pop_qm_et1);
            popFixEt2 = (EditText) view.findViewById(R.id.pop_qm_et2);
            popTv1 = (TextView) view.findViewById(R.id.pop_qm_tv1);
            checkBox1 = (CheckBox) view.findViewById(R.id.pop_qm_cb1);
            checkBox2 = (CheckBox) view.findViewById(R.id.pop_qm_cb2);
            checkBox3 = (CheckBox) view.findViewById(R.id.pop_qm_cb3);
            layout = (LinearLayout) view.findViewById(R.id.pop_qm_lay1);

            //设置默认组装名称
            String name = MyApp.currProject.getProject_name() + "：" + MyApp.currExploration.getNumber();
            popFixEt1.setText(name);

            HeadSecondControlPanel headsecond_layout = (HeadSecondControlPanel) view.findViewById(R.id.setpoint_layout);
            headsecond_layout.initHeadPanel(DataUtil.getsecondbean("保存", View.VISIBLE, R.color.bar_right, null,
                    "速成组装", R.color.title, View.VISIBLE, IconSourse.BACK + "", View.VISIBLE));
            //返回
            headsecond_layout.mleftTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    beanList = DataUtil.getBitTypeData(1);
                    list = new ArrayList<>();
                    for (BitTypeTabItemBean bean : beanList) {
                        list.add(bean.getName());
                    }
                    showPopupWindow();
                }
            });
            //保存
            headsecond_layout.setClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String ptid = "";//典设类型ID
                    String name = String.valueOf(popFixEt1.getText()).trim();
                    String b_hao = String.valueOf(popFixEt2.getText()).trim();
                    String type = String.valueOf(popTv1.getText()).trim();
                    if (name.isEmpty()) {
                        ToastShow.showShort(activity, "名称不能为空");
                        return;
                    }
                    if (type.isEmpty()) {
                        ToastShow.showShort(activity, "类型不能为空");
                        return;
                    }
                    if (!checkBox1.isChecked() && !checkBox2.isChecked() && !checkBox3.isChecked()) {
                        ToastShow.showShort(activity, "适用不能为空");
                        return;
                    }
                    dismiss();
                    //关键字
                    String keyword = getCheckBoxData();
                    if (beanList != null) ptid = beanList.get(list.indexOf(type)).getId();

                    RequestParams params = new RequestParams();
                    params.addBodyParameter("pro_name", MyApp.currProject.getProject_name());
                    params.addBodyParameter("pro_id", MyApp.currProject.getStage_id());//阶段id
                    params.addBodyParameter("exploration_id", MyApp.currExploration.getExploration_id());
                    params.addBodyParameter("codesetname", name);
                    params.addBodyParameter("codesetnumber", b_hao);
                    params.addBodyParameter("ptid", ptid);
                    params.addBodyParameter("keyword", keyword);
                    MyApp.HTTP.CreateProMoel2(handler, params, 6);
                }
            });
        }

        private String getCheckBoxData() {
            String keyword = "";
            if (checkBox1.isChecked())
                keyword = checkBox1.getText().toString().trim();
            if (checkBox2.isChecked()) {
                if (checkBox1.isChecked())
                    keyword = keyword + "," + checkBox2.getText().toString().trim();
                else
                    keyword = checkBox2.getText().toString().trim();
            }
            if (checkBox3.isChecked()) {
                if (checkBox2.isChecked())
                    keyword = keyword + "," + checkBox3.getText().toString().trim();
                else
                    keyword = checkBox3.getText().toString().trim();
            }
            return keyword;
        }

        private void showPopupWindow() {
            View myView = LayoutInflater.from(activity).inflate(R.layout.listview, null);
            AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
            dialog.setView(myView).create();
            final AlertDialog show = dialog.show();
            ListView lv = (ListView) myView.findViewById(R.id.lv);

            ArrayAdapter<String> adapter = new ArrayAdapter<>(activity, R.layout.textview, list);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    popTv1.setText(list.get(position));
                    show.cancel();
                }
            });
        }

    }
}
